package com.niit.onlinemusicserver.config;

import com.niit.onlinemusicserver.constant.Constants;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
            //当Credentials为true时，Origin不能为星号，需为具体的ip地址【如果接口不带cookie,ip无需设成具体ip】
            .allowedOrigins("http://localhost:4000, http://localhost:4001, http://localhost:5173")
            .allowedMethods("GET","POST","PUT","DELETE","OPTIONS","HEAD","PATCH")
            .allowedHeaders("*")
            .maxAge(3600)
            .allowCredentials(true);
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // 映射自定义目录到URL路径
//        registry.addResourceHandler("/files/**")
//                .addResourceLocations("file:./custom-files/")
//                .setCachePeriod(3600);

    // 添加多个资源位置
    registry.addResourceHandler("/assets/**")
            .addResourceLocations("classpath:/static/assets/", "classpath:/public/assets/")
            .setCachePeriod(0);
    registry.addResourceHandler("/img/avatorImages/**")
            .addResourceLocations(Constants.AVATOR_IMAGES_PATH);
    registry.addResourceHandler("/img/singerPic/**")
            .addResourceLocations(Constants.SINGER_PIC_PATH);
    registry.addResourceHandler("/img/songPic/**")
            .addResourceLocations(Constants.SONG_PIC_PATH);
    registry.addResourceHandler("/song/**")
            .addResourceLocations(Constants.SONG_PATH);
    registry.addResourceHandler("/img/songListPic/**")
            .addResourceLocations(Constants.SONGLIST_PIC_PATH);
    registry.addResourceHandler("/img/swiper/**")
            .addResourceLocations(Constants.BANNER_PIC_PATH);
    // 映射外部目录
//        registry.addResourceHandler("/external/**")
//                .addResourceLocations("file:/opt/application/files/");
  }

}